package com.genntii.examManager.common.security;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.genntii.examManager.common.constant.ExceptionConstant;
import com.genntii.examManager.common.exception.UserNotFoundException;
import com.genntii.examManager.domain.entity.User;
import com.genntii.examManager.mapper.UserMapper;
import jakarta.annotation.Resource;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

@Component
public class DBUserManager implements UserDetailsService {

    @Resource
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User u = userMapper.selectOne(new QueryWrapper<User>().eq("username",username));
        if (u == null){
            throw new UserNotFoundException(ExceptionConstant.USER_NOT_FOUND);
        }else {
            return new UserSecurity(u);
        }
    }
}
